package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Area;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.service.IAreaService;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.SpringUtil;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-18
 */
@RestController
public class AreaController {

    private static final Logger logger = LoggerFactory.getLogger(AreaController.class);

    @Autowired
    private IAreaService areaService;

    // ************* bellow is mechant interface for user *********

    @RequestMapping("/{mcode}/area/add")
    @ResponseBody
    public JsonDO addrAdd(@Valid Area area,
                          @PathVariable("mcode") String mcode,
                          BindingResult bindingResult) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        area.insert();
        return json;
    }

    @RequestMapping("/{mcode}/area/modify")
    @ResponseBody
    public JsonDO addrUpdate(@Valid Area area,
                             @PathVariable("mcode") String mcode,
                             BindingResult bindingResult) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        area.updateById();
        return json;
    }

    @RequestMapping("/{mcode}/area/detail")
    @ResponseBody
    public JsonDO getAreaDetailById(@Param("token") String token,
                                    @Param("id") long id
    ) {
        JsonDO json = new JsonDO();
        Area area = areaService.selectById(id);
        json.setData(area);
        return json;
    }


    public static String getAreaName(String code) {

        if (Constants.AREA_MAP.keySet().size() == 0) {
//            IAreaService areaService = (IAreaService) SpringUtil.getBean("areaService");
            IAreaService areaService = (IAreaService) SpringUtil.getBean(IAreaService.class);
            List<Area> list = areaService.selectByMap(new HashMap<String, Object>());
            for (Area item : list) {
                Constants.AREA_MAP.put(item.getCode(), item.getName());
            }
        }

        return Constants.AREA_MAP.get(code);
    }

    @RequestMapping("/test/area/list")
    @ResponseBody
    public Jinfo testlist(HttpServletRequest req) {
        Jinfo json = new Jinfo();

        List<Area> sheng = areaService.selectList(new EntityWrapper<Area>().setSqlSelect("code,sheng,di,xian,name,level")
                .orderBy("code", true));
        json.setInfo(sheng);

        return json;
    }

    @RequestMapping("/api/area/sheng")
    @ResponseBody
    public Jinfo sheng(HttpServletRequest req) {
        Jinfo json = new Jinfo();

        List<Area> sheng = areaService.selectList(new EntityWrapper<Area>().eq("level", 1));
        json.setInfo(sheng);

        return json;
    }

    @RequestMapping("/api/area/shi")
    @ResponseBody
    public Jinfo shi(HttpServletRequest req, @Param("code") String code) {
        Jinfo json = new Jinfo();
        if (Strings.isNullOrEmpty(code)) {
            json.setCode(4);
            json.setMsg("param error");
            return json;
        }
        String sheng = code.substring(0, 2);
        List<Area> shi = areaService.selectList(new EntityWrapper<Area>().eq("level", 2).eq("sheng", sheng));
        json.setInfo(shi);

        return json;
    }

    @RequestMapping("/api/area/xian")
    @ResponseBody
    public Jinfo xian(@Param("code") String code) {
        Jinfo json = new Jinfo();
        if (Strings.isNullOrEmpty(code)) {
            json.setCode(4);
            json.setMsg("param error");
            return json;
        }

        String sheng = code.substring(0, 2);
        String di = code.substring(2, 4);

        List<Area> shi = areaService.selectList(new EntityWrapper<Area>().eq("level", 3).eq("sheng", sheng).eq("di", di));
        json.setInfo(shi);

        return json;
    }

}
